package com.zh.note.huawei.onehundred.VLAN资源池;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        // String s1 = s.nextLine();
        String s1 = "20-21,15,18,30,5-10";
        int vlanNeed = 10;
        String[] vlans = s1.split(",");
        // int vlanNeed = s.nextInt();

        // 字符串中所有包含的数字加入list
        List<Integer> myList = new ArrayList<>();
        // 数字数组存储起来
        for (String vlan : vlans) {
            String[] temp = vlan.split("-");
            if (temp.length > 1) {
                for (int i = Integer.parseInt(temp[0]); i <= Integer.parseInt(temp[1]); i++) {
                    myList.add(i);
                }
            } else {
                myList.add(Integer.parseInt(temp[0]));
            }
        }
        // 若list包含申请的vlan，从list删除
        for (int i = 0; i < myList.size(); ++i) {
            if (myList.get(i) == vlanNeed) { //先找到对应索引，字符串的话用equals判断相等
                myList.remove(i);
            }
        }
        // 数组排序
        Collections.sort(myList);
        StringBuilder stringBuilder = new StringBuilder();
        // 对排序后的数组进行重组，遍历，若下个数等于上个数加1，继续遍历，否则直接加入stringBuilder
        int i = 0;
        while (i < myList.size()) {
            int firstNum = myList.get(i);
            int j = 1;
            while (j <= myList.size() - 1 - i) {
                if (myList.get(i) + j == myList.get(i + j)) {
                    j++;
                } else {
                    break;
                }
            }
            if (j == 1) { // 如果下个数不等于上个数加1，直接加入stringBuilder,同时++i
                stringBuilder.append(firstNum);
                stringBuilder.append(",");
                ++i;
            } else { // 否则第一个数-递增的最后一个数加入stringBuilder,同时i=i+j
                stringBuilder.append(firstNum + "-" + (firstNum + j - 1) + ",");
                i = i + j;
            }
        }
        System.out.println(stringBuilder.toString().substring(0, stringBuilder.length() - 1)); // 去除最后一个","
    }
}
